#include <QMouseEvent>
#include <QEvent>
#include "myslider.h"

extern "C" {
#include "sandcat.h"
}

MySlider::MySlider(QString control, QWidget *parent) :
    QSlider(parent)
{
    this->control = control;
    this->doNotRefresh = false;
}

void MySlider::mouseReleaseEvent( QMouseEvent * event )
{
    if(event->button() == Qt::LeftButton)
    {
        int x = event->pos().x();
        if( x < 0) x = 0;
        if( x > this->width() ) x = this->width();
        int position = (int)( (double)x / (double)this->width() * (this->maximum() - this->minimum())  );
        if(control == tr("position"))
            audio_seek( false, position );
        if(control == tr("volumn"))
            audio_volume( false, position, false );
    }
    this->doNotRefresh = false;
}

void MySlider::mousePressEvent( QMouseEvent * event )
{
    this->doNotRefresh = true;
}

void MySlider::mouseMoveEvent( QMouseEvent * event )
{
        int position = (int)( (double)event->pos().x() / (double)this->width() * (this->maximum() - this->minimum()) );
        setValue(position);
}

void MySlider::refresh()
{
    if(doNotRefresh) return;

    if(control == tr("position"))
        setValue( STATUS_position() );
    if(control == tr("volumn"))
        setValue( STATUS_volume() );

}
